//
//  Deck.m
//  PrefCore
//
//  Created by Sergey on 1/8/14.
//  Copyright (c) 2014 Play-on-smart. All rights reserved.
//

#import "PSDeck.h"

@interface PSDeck () {
    PSCard *cards[PS_DECK_SIZE];
}

-(instancetype)init;

@end

@implementation PSDeck

-(instancetype)init {
    if (self = [super init]) {
        for (int i = 0 ; i != PS_DECK_SIZE ; i++) {
            cards[i] = [PSCard cardWithIndex:i+1];
        }
    }
    return self;
}

+(instancetype)instance {
    static PSDeck *inst = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        inst = [[PSDeck alloc] init];
    });
    return inst;
}

-(PSCard*)cardAtIndex:(PSCardIndex)index {
    return cards[index];
}

-(void)shuffle {
    // Fisher–Yates - 7 times
    for (int p = 0; p < 7; ++p) {
        for (int i = PS_DECK_SIZE - 1; i > 0; i--) {
            int j = arc4random_uniform(i+1);
            if (i != j) {
                PSCard *tmp = cards[i];
                cards[i] = cards[j];
                cards[j] = tmp;
            }
        }
    }
}

@end
